#include "contacts.pb.h"
#include <fstream>
#include "../exception.hpp"
class ContactsManager
{
public:
    void ReadContacts(contacts::Contacts *contacts)
    {
            //这里路径不存在不会自动创建除了文件可以（路径的话用c++17d的filesystem）
        std::fstream input("contacts.bin", ios::in | ios::binary);

         
        if (!input)
        {
            cout << "---> ReadContacts: " << "contacts.bin" << ": File not found.  Creating a new file." << endl;
        }
        else if (!contacts->ParseFromIstream(&input))
        {
            input.close();
            throw Exception("ReadContacts: Failed to parse contacts.");
        }
        input.close();
    }
    void InsertContact(contacts::Contacts &contacts)
    {
        
        std::fstream output("contacts.bin", ios::out | ios::trunc | ios::binary);//默认截断到0字节然后增加
        if (!contacts.SerializeToOstream(&output))
        {
            output.close();
            throw Exception("InsertContacts: Failed to write contacts.");
        }
        output.close();
    }
};
